package com.base.common.utils;

import static com.base.common.GlobaConstants.ASYNCHRONOUS;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.StrutsStatics;

import com.base.common.bean.ActionResult;
import com.base.common.datesource.MultipleDataSource;
import com.base.common.json.JsonUtil;
import com.base.common.persistent.dao.VillageConfigMapper;
import com.cjy.common.bean.OpAttributes;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class CheckUserInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 1L;

	protected ActionResult actionResult;
	
	@Resource
	private VillageConfigMapper villageConfigMapper;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {

		System.out
				.println("-----------------------你好，你来到了拦截器----------------------------");

		System.out.println(invocation.getProxy().getMethod());

		HttpServletRequest httpServletRequest = (HttpServletRequest) invocation
				.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);

		// System.out.println(httpServletRequest.getSession().getAttribute("code"));

		// httpServletRequest.getSession().setAttribute("code", 100);
		
		try {
			if (isNeedValidSession(invocation.getProxy().getMethod())) {
				if (httpServletRequest.getSession() != null
						&& httpServletRequest.getSession().getAttribute("users") != null) {
					System.out.println(httpServletRequest.getParameter("OAflag"));
					if("1".equals(httpServletRequest.getParameter("OAflag"))){
						String compoundsIdStr = httpServletRequest.getParameter("compoundsId");
						if(compoundsIdStr != null && !"".equals(compoundsIdStr.trim())){
							System.out.println("进入check OA...");
							String dataSourceKey = "oa" + villageConfigMapper.getDBNum(Integer.valueOf(compoundsIdStr.trim())).trim() + "DataSource";
							System.out.println("切换数据库" + dataSourceKey);
							MultipleDataSource.setDataSourceKey(dataSourceKey);
							invocation.invoke();
							MultipleDataSource.setDataSourceKey("sqlserverDataSource");
						} else {
							actionResult = ActionResult.getActionResult(new OpAttributes()
									.add("code", 0));
							// 设置json返回值
							getJsonResult(invocation);
						}
					} else {
						invocation.invoke();
					}
					
				} else {
					actionResult = ActionResult.getActionResult(new OpAttributes()
							.add("code", -1));
					// 设置json返回值
					getJsonResult(invocation);
				}

			} else {
				invocation.invoke();
			}
		} catch (Exception e) {
			e.printStackTrace();
			actionResult = ActionResult.getActionResult(new OpAttributes()
					.add("code", 0));
			// 设置json返回值
			getJsonResult(invocation);
		}
		

		return ASYNCHRONOUS;
	}

	/**
	 * @param invocation
	 * @throws IOException
	 */
	public void getJsonResult(ActionInvocation invocation) throws IOException {
		HttpServletResponse httpServletResponse = (HttpServletResponse) invocation
				.getInvocationContext().get(StrutsStatics.HTTP_RESPONSE);
		PrintWriter printWriter = httpServletResponse.getWriter();
		printWriter.println(JsonUtil.dto2String(actionResult.getData()));
		printWriter.flush();
		printWriter.close();
	}

	/**
	 * 验证是否需要验证session
	 * 
	 * @return
	 */
	public boolean isNeedValidSession(String methodName) {
		return !(methodName.equals("getLatestVersion") || methodName.equals("login")
				|| methodName.equals("registe")
				|| methodName.equals("employeeLogin")
				|| methodName.equals("employeeRegiste")
				|| methodName.equals("queryAll")
				|| methodName.equals("getUnCheckAnnualMeetingRecord")
				|| methodName.equals("queryMeeingConfig")
				|| methodName.equals("getCaptcha")
				|| methodName.equals("updateCode")
				|| methodName.equals("getEmchatUserInfo")
				|| methodName.equals("subTest")
				|| methodName.equals("getShopsList")
				|| methodName.equals("getShopsListCounts")
				|| methodName.equals("getNewsList")
				|| methodName.equals("getNewsListCounts")
				|| methodName.equals("getSpecialShopList")
				|| methodName.equals("queryEstimationLevelList")
				|| methodName.equals("evaluate")
				|| methodName.equals("queryCurrentEmployeeInfo")
				|| methodName.equals("subPingfenHeadPic"));
	}

}
